import time

from appium import webdriver

from Commen.Event_Integration import Event
from Commen.log_logger import MyLog
from pagelocator import locator
from Commen.play_voice import play_music
from Commen.Types_operations import *
from Commen import Types_operations
from Commen import project_path

my_log = MyLog()


def iBed():
    desired_caps = {}
    desired_caps['platformName'] = 'Android'  # 测试平台
    desired_caps['platformVersion'] = '6.0'  # 平台版本
    desired_caps['deviceName'] = 'Producing_Voice'
    # desired_caps['app'] = 'xx.apk'  #apk文件路径名,如果设备还没有此应用,则会安装。
    desired_caps['appPackage'] = 'com.izhaohu.smartbed'  # 包名
    desired_caps['appActivity'] = 'com.izhaohu.smartbed.ui.LoadingActivity'  # 启动页名
    # desired_caps['unicodeKeyboard'] = True
    desired_caps['noReset'] = True  # 保证了app 测试前不会清除数据，缺省是会清除数据的，
    desired_caps['fullReset'] = 'false'  # 重置
    desired_caps['newCommandTimeout'] = 6000  # 超时时间

    # 启动Remote RPC与appium server进行连接 并发送
    driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
    driver.implicitly_wait(10)
    try:
        # 点击我的
        time.sleep(40)
        driver.find_element_by_xpath(locator.my_locator).click()
        my_log.info(Event_Click + '-' + My + '-我的')

        # 点击WiFi icon
        driver.find_element_by_xpath(locator.wifi_icon).click()
        my_log.info(Event_Click + '-' + WiFi_icon + '-wifi_icon')

        time.sleep(5)

        # 点击返回
        driver.find_element_by_xpath(locator.back).click()
        my_log.info(Event_Click + '-' + Wifi_Back + '-wifi页返回按钮')

        # 点击我的
        driver.find_element_by_xpath(locator.my_locator).click()
        my_log.info(Event_Click + '-' + My + '-我的')

        # 网络不稳定 设置等待时间
        time.sleep(35)

        # 点击测试按钮
        driver.find_element_by_xpath(locator.click_button).click()
        my_log.info(Event_Click + '-' + Test_Button + '-测试按钮')

        time.sleep(20)
        # 点击挂断
        driver.find_element_by_xpath(locator.click_up).click()
        my_log.info(Event_Click + '-' + Up + '-挂断')

        time.sleep(5)

        # 模拟语音呼叫
        # Analog_speech('"小呼 小呼"')
        for i in range(500):
            time.sleep(5)
            # play_music(file_name=project_path.xh)
            # my_log.info(Types_operations.Music + '-' + Types_operations.Awaken + '-播放了唤醒语音')
            # time.sleep(8)
            play_music(file_name=project_path.get_up)
            my_log.info(Types_operations.Music + '-' + Types_operations.Get_up + '-播放了起床语音')
            time.sleep(10)
            Event(driver).event()
            time.sleep(10)
            play_music(file_name=project_path.sleep)
            my_log.info(Types_operations.Music + '-' + Types_operations.Sleep + '-播放了睡觉语音')
            time.sleep(10)
            play_music(file_name=project_path.weather)
            my_log.info(Types_operations.Music + '-' + Types_operations.Weather + '-播放了天气')
            time.sleep(20)
            # 点击返回
            try:
                driver.find_element_by_xpath(locator.water_back).click()
                my_log.info(Event_Click + '-' + Water_Back + '-天气页返回按钮')
            except Exception as e:
                my_log.info('出错啦,错误是{}'.format(e))
            play_music(file_name=project_path.traditional_opera)
            my_log.info(Types_operations.Music + '-' + Types_operations.Traditional + '-播放了戏曲')
            time.sleep(120)
            play_music(file_name=project_path.radio_broadcast)
            my_log.info(Types_operations.Music + '-' + Types_operations.Radio_broadcast + '-播放了广播')
            time.sleep(10)
            play_music(file_name=project_path.headache)
            my_log.info(Types_operations.Music + '-' + Types_operations.Health_Lecture + '-播放了健康讲座')
            time.sleep(180)
            play_music(file_name=project_path.god)
            my_log.info(Types_operations.Music + '-' + Types_operations.Search_business + '-播放了封神榜')
            time.sleep(60)
            play_music(file_name=project_path.egg)
            my_log.info(Types_operations.Music + '-' + Types_operations.Egg + '-播放了番茄炒蛋')
            time.sleep(60)
            play_music(file_name=project_path.unrecognizable)
            my_log.info(Types_operations.Music + '-' + Types_operations.Distinguish + '-播放了无法识别语音')
            time.sleep(30)
            play_music(file_name=project_path.speak)
            my_log.info(Types_operations.Music + '-' + Types_operations.Speak + '-播放了未说话语音')
            time.sleep(30)

    except Exception as e:
        my_log.info('出错啦,错误是{}'.format(e))
